package com.lening.utils;

import com.lening.entity.StuBean;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class MyReadXlx {

	public static List<StuBean> readXls(InputStream is) throws IOException {
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is); 
        StuBean bean = null; 
        List<StuBean> list = new ArrayList<StuBean>(); 
        // 循环工作表Sheet 

            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);

            // 循环行Row行，从1开始，第0行是标题，不要
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) { 
                HSSFRow hssfRow = hssfSheet.getRow(rowNum); 
                bean = new StuBean();

             // 用户名,也是从1开始获取，不要id，id的下标是0
                HSSFCell sname = hssfRow.getCell(1);
                if (sname == null) {
                    continue; 
                }

                bean.setSname(sname.getStringCellValue());

             // 地址
                HSSFCell address = hssfRow.getCell(2);
                if (address == null) {
                    continue; 
                }
                bean.setAddress(address.getStringCellValue());
                list.add(bean);
            } 

        return list; 
    } 
}
